(function () {

    "use strict";

    var api = {
        addAddress: "/api/address/add"
    };
    sm.controller('wx-center-address-add-controller', ['$scope', '$http', 'Modal', 'Toast', function ($scope, $http, Modal, Toast){

        var search = window.location.search;
        var s = GetQueryString('s');

        $scope.addr = {
            name: '',
            phone: '',
            province: '',
            city: '',
            area: '',
            address: '',
            default: false
        };

        $scope.selects = {
            province: [],
            cities: [],
            area: []
        };

        $scope.cities = cities;
        angular.forEach($scope.cities, function (province) {
            province.value = province.text;
            angular.forEach(province.children, function (city) {
                city.value = city.text;
                angular.forEach(city.children, function (area) {
                    area.value = area.text
                });
            });
        });

        $scope.$watch('addr.province', function (newValue, oldValue) {

            if (newValue === oldValue) {
                return false;
            }

            angular.forEach($scope.cities, function (val) {
                if (newValue == val.text) {
                    $scope.selects.cities = val.children;
                }
            });

            $scope.addr.city = '';
            $scope.selects.area = [];

        });

        $scope.$watch('addr.city', function (newValue, oldValue) {

            if (newValue === oldValue) {
                return false;
            }

            angular.forEach($scope.selects.cities, function (val) {
                if (newValue == val.text) {
                    $scope.selects.area = val.children;
                }
            });

        });


        // 保存
        $scope.save = function () {

            if (!checkParams()) {
                return false;
            }

            Toast.loading({title: "正在提交"});

            var data = angular.copy($scope.addr);
            data['default'] = data['default'] === true ? 1 : 0;
            $http.post(api.addAddress, data).success(function (res) {
                Toast.stop();
                if (res.state) {
                    if (!s) {
                        Modal.alert({
                            title: '成功提醒',
                            content: '收货地址添加成功',
                            buttons: [{
                                name: '取消'
                            }, {
                                name: '返回地址列表',
                                event: function () {
                                    location.href="/address"
                                }
                            }]
                        });
                    } else {
                        location.href="/order/confirm?addressId=" + res.data.id
                    }
                } else {
                    Toast.error({title: res.error || '添加失败'});
                }
            }).error(function(error) {
                Toast.stop();
                Toast.error();
            });
        };

        function checkParams() {
            if (!$scope.addr.name || !$scope.addr.phone || !$scope.addr.province || !$scope.addr.city || !$scope.addr.area
                || !$scope.addr.address) {
                Modal.alert({
                    title: '错误提醒',
                    content: '有必填项没有填写,请填写完整.'
                });
                return false;
            }

            if (!/^0\d{2,3}-?\d{7,8}$/.test($scope.addr.phone)
                && !/^400-([0-9]){1}([0-9-]{7})$/.test($scope.addr.phone)
                && !/^1[34578]\d{9}$/.test($scope.addr.phone)) {
                Modal.alert({
                    title: '错误提醒',
                    content: "电话号码格式错误"
                });
                return false;
            }
            return true;
        }

    }]);
})();

